using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Hmw;
using net_work.IService.Hmw;

namespace net_work.Controllers.Hmw;

[Route("api/[controller]")]
[ApiController]
public class HomeworkQuestionController(
    ILogger<HomeworkQuestionController> logger,
    IHomeworkQuestionService homeworkQuestionService) : ControllerBase
{
    /// <summary>
    /// 获取作业题目列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListHomeworkQuestions([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取作业题目列表");
        var result = await homeworkQuestionService.GetHomeworkQuestionsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取作业题目信息
    /// </summary>
    /// <param name="homeworkQuestionId">作业题目ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetHomeworkQuestionById([FromQuery] long homeworkQuestionId)
    {
        logger.LogInformation("获取作业题目信息");
        return await homeworkQuestionService.SelectByIdAsync(homeworkQuestionId) is { } homeworkQuestion
            ? AjaxResult.Success(homeworkQuestion)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增作业题目
    /// </summary>
    /// <param name="homeworkQuestionAddRequest">新增作业题目实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddHomeworkQuestion(
        [FromBody] HomeworkQuestionAddRequest homeworkQuestionAddRequest)
    {
        logger.LogInformation("新增作业题目");
        var affectRows = await homeworkQuestionService.InsertHomeworkQuestion(homeworkQuestionAddRequest);
        return affectRows > 0
            ? AjaxResult.Success(new { affectRows })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改作业题目
    /// </summary>
    /// <param name="homeworkQuestionUpdateRequest">修改作业题目实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateHomeworkQuestion(
        [FromBody] HomeworkQuestionUpdateRequest homeworkQuestionUpdateRequest)
    {
        logger.LogInformation("修改作业题目");
        return await homeworkQuestionService.UpdateHomeworkQuestion(homeworkQuestionUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除作业题目
    /// </summary>
    /// <param name="ids">作业题目ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteHomeworkQuestion([FromBody] List<long> ids)
    {
        logger.LogInformation("删除作业题目");
        return await homeworkQuestionService.DeleteHomeworkQuestionByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}